//
//  LCMyTaskViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/23.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCMyTaskViewController.h"
#import "SPPageMenu.h"
#import "LCTaskService.h"

#import "LCMyTaskListViewController.h"
#import "LCMyTaskGameViewController.h"
#import "XWXAllXieYiController.h"

@interface LCMyTaskViewController ()<SPPageMenuDelegate>

@property (strong, nonatomic) NSArray * titleArr;

@property (weak, nonatomic) IBOutlet UIView *titleBgView;

@property (nonatomic, strong) SPPageMenu *pageMenu;

@property (weak, nonatomic) IBOutlet UIScrollView *contentView;

@property (strong, nonatomic)LCUnionModel * dyUnionModel;

@property (strong, nonatomic)LCUnionModel * syUnionModel;

@end

@implementation LCMyTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.titleBgView addSubview:self.pageMenu];
    [self addChildController];
    [self getUnionData];
}

-(void)addChildController {
    self.contentView.width = ScreenW;
    self.contentView .contentSize = CGSizeMake(self.titleArr.count*ScreenW, 0);
    for (int i = 0; i < self.titleArr.count; i++) {
        LCMyTaskListViewController * listVC = [LCMyTaskListViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
        listVC.status = [self statusForIndex:i];
        listVC.view.frame = CGRectMake(ScreenW * i, 0, ScreenW, CGRectGetHeight(self.contentView.frame));
        [self.contentView addSubview:listVC.view];
        [self addChildViewController:listVC];
    }
}

-(NSString *)statusForIndex:(NSInteger)index {
    switch (index) {
        case 0: {
            return @"waiting";
        } break;
        case 1: {
            return @"examine";
        } break;
        case 2: {
            return @"reward";
        } break;
        case 3: {
            return @"cancel";
        } break;
        default: {
            return @"";
        } break;
    }
}

-(NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"进行中",@"待审核",@"已通过",@"未通过"];
    }
    return _titleArr;
}

-(SPPageMenu *)pageMenu {
    if (!_pageMenu) {
        _pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, ScreenW, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
        _pageMenu.delegate = self;
        [_pageMenu setItems:self.titleArr selectedItemIndex:0];
        _pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
        _pageMenu.itemTitleFont = [UIFont systemFontOfSize:12];
        _pageMenu.selectedItemTitleColor = [UIColor colorWithHexString:@"#000000"];
        _pageMenu.unSelectedItemTitleColor = [UIColor colorWithHexString:@"#999999"];
        _pageMenu.bridgeScrollView = self.contentView;
        _pageMenu.tracker.backgroundColor = [UIColor colorWithHexString:@"#16A1E3"];
        _pageMenu.trackerWidth = 40;
        _pageMenu.dividingLine.hidden = YES;
    }
    return _pageMenu;
}

-(void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (!self.contentView.isDragging) {
        if (labs(toIndex-fromIndex) >= 2) {
            [self.contentView setContentOffset:CGPointMake(ScreenW * toIndex, 0) animated:NO];
        } else {
            [self.contentView setContentOffset:CGPointMake(ScreenW * toIndex, 0) animated:YES];
        }
    }
}

- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getUnionData {
    [LCTaskService getUnionDataWithBlock:^(BOOL isSuccess, NSArray<LCUnionModel *> *list) {
        if (isSuccess) {
            for (LCUnionModel * unionM in list) {
                if ([unionM.unionmodel isEqualToString:@"闪游"]) {
                    self.syUnionModel = unionM;
                }
                if ([unionM.unionmodel isEqualToString:@"多游"]) {
                    self.dyUnionModel = unionM;
                }
            }
        }
    }];
}

- (IBAction)dyTaskBtnAction:(UIButton *)sender {
    LCMyTaskGameViewController * pushVC = [LCMyTaskGameViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
    pushVC.dyUnionModel = self.dyUnionModel;
    [self.navigationController pushViewController:pushVC animated:YES];
}

- (IBAction)syTaskBtnAction:(UIButton *)sender {
    NSString * channelId = self.syUnionModel.config.iosappid;
    NSString * userId = kFORMAT(@"%@",LCUserDefGet(Userid));
    NSString * sign = [LCFunction ns_md5:kFORMAT(@"%@-%@:sdkUserId=%@&",channelId, self.syUnionModel.config.iosappsecret,userId)].uppercaseString;
    
    XWXAllXieYiController *webVC = [XWXAllXieYiController new];
    webVC.tileStr = @"任务二区";
    webVC.urlStr = MyTaskAreaListWeb(channelId, userId, sign);
    webVC.isPushFrom = YES;
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
